package com.niit.order.web;

import com.niit.feign.pojo.User;
import com.niit.order.pojo.Order;
import com.niit.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private OrderService orderService;
   @Autowired
   private RestTemplate restTemplate;

    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId,
                                    @RequestHeader(value = "Description",required = false)String description
                                    ) {
        System.out.println("Description: " + description);

        // 根据id查询订单并返回
        Order order = orderService.queryOrderById(orderId);
        //通过RestTemplate查询用户信息 使用服务名代替ip和端口号
        String url = "http://userservice/user/"+order.getUserId();
        User forObject = restTemplate.getForObject(url, User.class);
        //封装返回的数据
        order.setUser(forObject);
        return order;
    }
}
